using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Woz
{
    public partial class Toolbox : UserControl
    {
        public Toolbox()
        {
            InitializeComponent();

            // add a callback to the resize event
            this.Resize += new EventHandler(this.cResize);
        }

        public void AddTool(Woz.Tool pTool)
        {
            // create a new toolbox button
            Woz.Toolbox.Button button = new Woz.Toolbox.Button(this.mFlowLayoutPanel, pTool);

            // add a callback so we know when its check
            button.MouseDown += new MouseEventHandler(this.cButtonMouseDown);

            // add it to the flow layout panel
            this.mFlowLayoutPanel.Controls.Add(button);

            // move the split container down
            this.mSplitContainer.SplitterDistance += Woz.Toolbox.Button.TotalHeight;
        }

        public void SelectTool(Woz.Tool pTool)
        {
            // make sure this tool exists
            if (!this.mFlowLayoutPanel.Controls.ContainsKey(pTool.Name))
            {
                // it doesnt exist so just leave
                return;
            }

            // this tool definetly exists

            // so uncheck the current one if we have one
            if (this.mSelectedButton != null)
            {
                this.mSelectedButton.Checked = false;
            }

            // then save this as the current one
            this.mSelectedButton = (Woz.Toolbox.Button)this.mFlowLayoutPanel.Controls[pTool.Name];

            // now check it
            this.mSelectedButton.Checked = true;

            // finally invoke the event
            if (this.SelectedToolChanged != null)
            {
                this.SelectedToolChanged.Invoke(this, pTool);
            }
        }

        private void cResize(object pSender, EventArgs pEventArgs)
        {
            this.mSplitContainer.SplitterDistance = 5 + this.mFlowLayoutPanel.Controls.Count * Woz.Toolbox.Button.TotalHeight;

            // resize all the buttons
            foreach (Control control in this.mFlowLayoutPanel.Controls)
            {
                control.Width = this.mFlowLayoutPanel.Width - 6;
            }
        }

        private void cButtonMouseDown(object pSender, MouseEventArgs pMouseEventArgs)
        {
            // get the button
            Woz.Toolbox.Button button = (Woz.Toolbox.Button)pSender;
            
            // check if this is the currently selected button
            if (button == this.mSelectedButton)
            {
                // this is already checked so just leave
                return;
            }

            // just call our other function to handle this now
            this.SelectTool((Woz.Tool)button.Tag);
        }

        private Woz.Toolbox.Button mSelectedButton;

        public delegate void ToolChangeHandler(object pSender, Woz.Tool pTool);
        public event ToolChangeHandler SelectedToolChanged;
    }
}
